package cn.com.czpb.client.module.video;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.model.news.NewsChannelBean;
import cn.com.czpb.client.model.news.NewsChannelEntity;
import cn.com.czpb.client.module.news.NewsBaseFragment;
import cn.com.czpb.client.module.video.adapter.FragmentVideoAdapter;
import cn.com.czpb.client.module.video.fragment.VideoItemFragment;
import cn.com.czpb.client.module.video.utils.VideoPlayerHelper;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/7/20.
 */

public class VideoFragmentMain extends BaseFragment{
    private List<NewsBaseFragment> mFragmentArrays;
    private List<String> mTabTitlesList;
    private List<String> mTabTitles;

    @BindView(R.id.video_tablayout)
    TabLayout tabLayout;
    @BindView(R.id.video_tab_viewpager)
    ViewPager viewpager;
    private FragmentVideoAdapter adapter;

    public static VideoFragmentMain newInstance() {
        VideoFragmentMain fragment = new VideoFragmentMain();
        return fragment;
    }
    @Override
    public int setMyContentView() {
        return R.layout.fragment_video_main;
    }
    @Override
    public void initView() {
        getVideoTabs();
        mFragmentArrays = new ArrayList<>();
        adapter = new FragmentVideoAdapter(getFragmentManager());
        mTabTitles = new ArrayList<String>();

        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                VideoPlayerHelper.getInstance().stop();
                NewsBaseFragment frag = (NewsBaseFragment) adapter.getItem(position);
                frag.init();
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });



    }

    private void getVideoTabs() {
        Map<String, String> map = new HashMap();
        map.put("siteid", InterfaceJsonfile.SITEID);
        Factory.provideHttpService().videoTypes(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NewsChannelEntity>() {
                    @Override
                    public void call(NewsChannelEntity entity) {
                        List<NewsChannelBean> lists =  entity.data.getCommon();

                        for(int i=0;i<lists.size();i++){
                            mTabTitles.add(lists.get(i).getCnname());
                        }
                        tabLayout.setTabMode(TabLayout.MODE_FIXED);
                        //设置tablayout距离上下左右的距离
                        //tab_title.setPadding(20,20,20,20);


                        for(int i=0;i<lists.size();i++){
                            NewsChannelBean bean = lists.get(i);
                            VideoItemFragment itemFragment = VideoItemFragment.newInstance(i, bean);
                            itemFragment.setTitle(bean.getCnname());
                            mFragmentArrays.add(itemFragment);
                        }
                        //将ViewPager和TabLayout绑定
//                        FragmentVideoAdapter adapter = new FragmentVideoAdapter(getFragmentManager(),mFragmentArrays,mTabTitles);
                        adapter.setData(mFragmentArrays,mTabTitles);
                        viewpager.setAdapter(adapter);
                        tabLayout.setupWithViewPager(viewpager);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("异常-->"+throwable.toString());
                    }
                });
    }

    @Override
    public void initData() {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
